package CreatorUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import LoginUtil.*;

/**
 * Servlet implementation class LoginServlet
 */
public class CreateNewUserServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, java.io.IOException {

try
{	 
	//UserBean user;
	if(request.getParameter("usertype").compareToIgnoreCase("patient")==0)
    {
		
		Patient user = new Patient(request.getParameter("usernamebox"), request.getParameter("passwordbox"), 
				request.getParameter("firstnamebox"), request.getParameter("lastnamebox"), request.getParameter("addressbox"), 
				request.getParameter("phonebox"), request.getParameter("emailbox"), request.getParameter("genderbox"),
				request.getParameter("pharmacybox"), request.getParameter("insurancebox"), request.getParameter("allergiesbox"),
				Integer.parseInt(request.getParameter("agebox")));
   	 	user.setType(4);
	   	user.setUserName(request.getParameter("usernamebox"));
	    user.setPassword(request.getParameter("passwordbox"));
	    user.setFirstName(request.getParameter("firstnamebox"));
	    user.setLastName(request.getParameter("lastnamebox"));
	    
	    //insert patient into database
	    // insert into database! yayyy
		if (UserDAO.insertNewPatient(user))
		{
	     response.sendRedirect("newUserCreated.jsp");
		}
		else
		{
			System.out.println("Some sort of error");
		}
	    
    }
	
	else
	{
	     UserBean user = new UserBean();
	     user.setUserName(request.getParameter("usernamebox"));
	     user.setPassword(request.getParameter("passwordbox"));
	     user.setFirstName(request.getParameter("firstnamebox"));
	     user.setLastName(request.getParameter("lastnamebox"));
	     if(request.getParameter("usertype").compareToIgnoreCase("doctor")==0)
	     {
	    	 user.setType(2);
	     }
	     else if(request.getParameter("usertype").compareToIgnoreCase("nurse")==0)
	     {
	    	 user.setType(3);
	     }
	     
	     if(UserDAO.insertNewDocNurse(user))
	     {
	    	 response.sendRedirect("newUserCreated.jsp");
	     }
	     else
			{
				System.out.println("Some sort of error");
			}
	}
     
    

} 
		
		
catch (Throwable theException) 	    
{
     System.out.println(theException); 
}
       }
	}

